package com.clusterswisdom.horn.api.tmall.config;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.dom4j.Element;

public class TmallApplication {
	
	private String name;
	
	private String className;
	
	private boolean defaultApplication;
	
	private boolean monitEvent;

	private String url;
	
	private String wsUrl;
	
	private String appKey;
	
	private String appSecret;
	
	private String sessionKey;
	
	private String refreshKey;
	
	private Long invalidateTime;
	
	public TmallApplication(Element element) {
		name = element.attributeValue("name");
		className = element.attributeValue("class");
		String defaultValue = element.attributeValue("default");
		defaultApplication = defaultValue != null && defaultValue.equalsIgnoreCase("true");
		
		String monitEventValue = element.attributeValue("monitEvent");
		monitEvent = monitEventValue != null && monitEventValue.equalsIgnoreCase("true");
		
		url = element.elementTextTrim("url");
		wsUrl = element.elementText("wsUrl");
		appKey = element.elementTextTrim("appKey");
		appSecret = element.elementTextTrim("appSecret");
		sessionKey = element.elementTextTrim("sessionkey");
		
		Properties properties = readConfig(this);
		if (properties != null) {
			if (sessionKey == null) {
				sessionKey = properties.getProperty("sessionKey");
			}
			
			refreshKey = properties.getProperty("refreshKey");
			String invalidateTimeStr = properties.getProperty("invalidateTime");
			if (invalidateTimeStr != null) {
				invalidateTime = Long.parseLong(invalidateTimeStr);
			}
		}
	}
	
	public String getName() {
		return name;
	}

	public String getClassName() {
		return className;
	}

	public boolean isDefaultApplication() {
		return defaultApplication;
	}

	public boolean isMonitEvent() {
		return monitEvent;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getWsUrl() {
		return wsUrl;
	}

	public void setWsUrl(String wsUrl) {
		this.wsUrl = wsUrl;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public String getRefreshKey() {
		return refreshKey;
	}

	public void setRefreshKey(String refreshKey) {
		this.refreshKey = refreshKey;
	}

	public Long getInvalidateTime() {
		return invalidateTime;
	}

	public void setInvalidateTime(Long invalidateTime) {
		this.invalidateTime = invalidateTime;
	}
	
	private Properties readConfig(TmallApplication application) {
		File file = new File(application.getName());
		if (file == null || !file.exists()) {
			return null;
		}
		
		Properties property = new Properties();
		try {
			property.load(new FileInputStream(file));
		
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return property;
	}
}
